$(function() {
	//回车键登录事件
	$('._img_yzm').keydown(function(event) {
		if(event.keyCode == 13) {
			$("._ptuser_login").click();
		}
	});

	$("#checkbox").click(function() {
		var that = $(this);
		if($("#checkbox").is(':checked')) {
			$("._ptuser_register").removeClass("not");
			$("._ptuser_register").removeAttr("disabled");
		} else {
			$("._ptuser_register").addClass("not");
			$("._ptuser_register").attr("disabled", "disabled");
		}
	});

	// 登录、注册输入框获得焦点删除错误提示样式
	$("._pt_login").focus(function() {
		var that = $(this);
		var id = that.attr('id');
		rm_login_error(id);
	});

	// 修改密码输入框获得焦点删除错误提示样式
	$("._resetpasswd_blur").focus(function() {
		var that = $(this);
		var id = that.attr('id');
		rm_resetpasswd_error(id);
	});

	// 修改密码输入框获得焦点删除错误提示样式
	$("._mailpasswd_blur").focus(function() {
		var that = $(this);
		var id = that.attr('id');
		rm_mailpasswd_error(id);
	});
	// 找回密码输入框获得焦点删除错误提示样式
	$("._backpasswd_blur").focus(function() {
		var that = $(this);
		var id = that.attr('id');
		rm_mailpasswd_error(id);
	});
	// 找回密码失去焦点判断参数有效性
	$("._backpasswd_blur").blur(function() {
		var that = $(this);
		var value = that.val(); //.attr('value');
		var id = that.attr('id');
		if(value == '') {
			add_mailpasswd_error(id, that.attr('opt-tip'));
			return;
		}

		if(!checkEmail(value)) {
			add_mailpasswd_error(id, '请输入正确的邮箱');
			return;
		}
	});

	// 邮箱重置密码失去焦点判断参数有效性
	$("._mailpasswd_blur").blur(function() {
		var that = $(this);
		var value = that.val(); //.attr('value');
		var id = that.attr('id');
		if(value == '') {
			add_mailpasswd_error(id, that.attr('opt-tip'));
			return;
		}

		if(!checkPasswd(value)) {
			add_mailpasswd_error(id, '请输入6-16位字符的密码');
			return;
		}
	});

	// 验证码 鼠标over的时候删除错误提示样式
	$("#captcha").mouseover(function() {
		yzm_login_error(false);
	});
	// 修改密码输入框失去焦点判断数据合法性
	$("._resetpasswd_blur").blur(function() {
		var that = $(this);
		var value = that.val(); //.attr('value');
		var id = that.attr('id');
		if(value == '') {
			add_resetpasswd_error(id, that.attr('opt-tip'));
			return;
		}

		if(!checkPasswd(value)) {
			add_resetpasswd_error(id, '请输入6-16位字符的密码');
			return;
		}
	});

	// 登录、注册输入框失去焦点判断数据合法性
	$("._blur_auth").blur(function() {
		var that = $(this);
		var value = that.val(); //.attr('value');
		var id = that.attr('id');
		if(value == '') {
			add_login_error(id, that.attr('opt-tip'));
			return;
		}
		var opt = that.attr('opt-type')
		if(opt == 'register') {
			// 注册增加判断邮箱和密码的规则
			if(id == 'email') {
				if(!checkEmail(value)) {
					add_login_error(id, "请输入正确的邮箱");
					return;
				}
			} else if(id == 'passwd') {
				if(!checkPasswd(value)) {
					add_login_error(id, '请输入6-16位字符的密码');
					return;
				}
			}
		}

	});

	// loding加载弹出
	function run_waitMe(login_effect, message) {
		message = message || '提交中...'
		$(login_effect).waitMe({
			effect: 'ios',
			bg: 'rgba(16,128,242,0)',
			text: message,
			color: '#fff',
			padding: '10px',
			sizeW: '',
			sizeH: '',
			source: 'img.svg'
		});
	}
	// 用户中心，修改密码，确认提交
	$('._reswtpasswd_submit').click(function() {
		var old_pwd = $('._old_passwd').val();
		var new_first_pwd = $('._new_first_passwd').val();
		var new_second_pwd = $('._new_second_passwd').val();
		if(stringIsNull(old_pwd) == true) {
			add_resetpasswd_error('src', '请输入密码');
		}

		if(stringIsNull(new_first_pwd) == true) {
			add_resetpasswd_error('new1', '请输入密码');
		}
		if(stringIsNull(new_second_pwd) == true) {
			add_resetpasswd_error('new2', '请输入密码');
		}

		if(haserror('_resetpasswd_blur')) {
			return;
		}

		if(!checkPasswd(old_pwd)) {
			add_resetpasswd_error('src', '请输入6-16位字符的密码');
			return;
		}
		if(!checkPasswd(new_first_pwd)) {
			add_resetpasswd_error('new1', '请输入6-16位字符的密码');
			return;
		}
		if(!checkPasswd(new_second_pwd)) {
			add_resetpasswd_error('new2', '请输入6-16位字符的密码');
			return;
		}

		if(new_first_pwd != new_second_pwd) {
			add_resetpasswd_error('new2', '请输入相同的密码');
			return;
		}

		var data = {};
		data.old_passwd = old_pwd;
		data.new_first_pwd = new_first_pwd;
		data.new_second_pwd = new_second_pwd;

		var that = $(this);
		var submit_url = that.attr('submit-url');
		var redirct_url = that.attr('redirect-url');

		var cur = $(this);
		// 按钮置为不可点击
		it_enable(cur, true);
		$('#register').text('');
		run_waitMe('#register', '修改中');

		$.ajax({
			url: submit_url,
			dataType: 'json',
			async: true,
			data: data,
			type: "POST",
			success: function(req) {
				it_enable(cur, false);
				$('#register').waitMe('hide');
				$('#register').text('确认');
				// 请求成功时处理
				if(req.status == 200) {
					// 修改成功
					show_msg({
						msg: req.msg,
						type: 'ok',
						time: 900
					});
					location.href = redirct_url;
				} else if(req.status == 1010) {
					location.reload();
				} else {
					if(req.status == 1) {
						add_resetpasswd_error("src", req.msg);
					} else if(req.status == 2) {
						add_resetpasswd_error("new1", req.msg);
					} else if(req.status == 3) {
						add_resetpasswd_error("new2", req.msg);
					} else {
						show_msg({
							msg: req.msg,
							type: 'error',
							time: 1500
						});
					}
				}
				return;
			},
			error: function() {
				it_enable(cur, false);
				$('#register').waitMe('hide');
				$('#register').text('确认');
				// 请求出错处理
				show_msg({
					msg: '系统异常',
					type: 'error',
					time: 1000
				});
			}
		});
	});

	//验证码框焦点移入，取消错误提示
	$('._img_yzm').on('focus', function() {
			$('._yzm_warning').hide()
		})
		//判断验证码是否为空
	function verifyCode() {
		var flag = false
		if($('._img_yzm').val() == "") {
			$('._yzm_warning .font').html('请输入验证码')
			$('._yzm_warning').show()
			flag = true
		}
		return flag
	}

	//点击更换验证码
	$('._img_update').on('click', function() {
			updateVerityCode()
		})
		//更新验证码
	function updateVerityCode() {
		$('._img_yzm')[0].value = ''
		$('._img_update')[0].src = '/member/imgverify.html?d=' + Math.random()
	}

	// 用户登录ajax
	$("._ptuser_login").click(function() {
		if(verifyCode()) {
			return
		}
		var user_email = $("#email").val();
		var passwd = $("#passwd").val();
		var verify = $('._img_yzm').val();
		var geetest_challenge = $(".geetest_challenge").val();
		var geetest_validate = $(".geetest_validate").val();
		var geetest_seccode = $(".geetest_seccode").val();
		var login_type = $("._user_login_type").val();
		var role = $("._37degree_role").val();
		if(user_email == '') {
			add_login_error('email', '请填写邮箱');
		}
		if(passwd == '') {
			add_login_error('passwd', '请填写密码');
		}

		var cur = $(this);
		// 按钮置为不可点击
		it_enable(cur, true);
		$('#waitMe_login').text('');
		run_waitMe('#waitMe_login', '登录中...');

		data = {
			'user_email': user_email,
			'passwd': passwd,
			'geetest_challenge': geetest_challenge,
			'geetest_validate': geetest_validate,
			'geetest_seccode': geetest_seccode,
			'login_type': login_type,
			'37degree_role': role,
			'verify': verify
		};

		$.ajax({
			url: '/member/login.html',
			type: 'post',
			dataType: 'json',
			async: true,
			data: data,
			success: function(req) {
				if(req.status == 200) {
					// 登录成功
					// todo 埋点--记录登录用户的账号
					//alert(data.user_email)
					zhuge.identify(data.user_email)
					login_success_direct(req.data);
				} else {
					it_enable(cur, false);
					$('#waitMe_login').waitMe('hide');
					$('#waitMe_login').text('登  录');
					if(req.status == 1) {
						add_login_error("email", req.msg);
					} else if(req.status == 2) {
						add_login_error("passwd", req.msg);
					} else {
						$('._yzm_warning .font').html(req.msg)
						$('._yzm_warning').show()
					}
					updateVerityCode()

				}
				//				reloadcaptcha();
				return;
			},
			error: function() {
				it_enable(cur, false);
				$('#waitMe_login').waitMe('hide');
				$('#waitMe_login').text('登  录');
				//				reloadcaptcha();
				// add_login_error("email", '系统异常');
				show_msg({
					msg: '系统异常',
					type: 'error',
					time: 1000
				});
			}
		});

	});

	// 用户注册ajax	---提交注册事件   	
	$("._ptuser_register").click(function() {

		var user_email = $("#email").val();
		var passwd = $("#passwd").val();

		var pt = 'us';

		if(user_email == '') {
			add_login_error('email', '请填写邮箱');
		}
		if(passwd == '') {
			add_login_error('passwd', '请填写密码');
		}

		if(haserror('_blur_auth')) {
			return;
		}

		if(!checkEmail(user_email)) {
			add_login_error('email', '请输入正确的邮箱');
			return;
		}

		if(!checkPasswd(passwd)) {
			add_login_error('passwd', '请输入6-16位字符的密码');
			return;
		}

		var cur = $(this);
		// 按钮置为不可点击
		it_enable(cur, true);
		$('#waitMe').html('');

		run_waitMe('#waitMe', '注册中...');
		data = {
			'email': user_email,
			'passwd': passwd,
			'pt': pt
		};

		$.ajax({
			url: '/member/register',
			type: 'post',
			dataType: 'json',
			async: true,
			data: data,
			success: function(req) {
				if(req.status == 200) {
					// 提交注册成功
					// TODO 埋点
					myCookie.addCookie('registerEmail', data.email)
					zhuge.track('注册_提交');

					register_success_direct(req.data);
					it_enable(cur, false);
					$('#waitMe').waitMe('hide');
				} else {
					it_enable(cur, false);
					$('#waitMe').waitMe('hide');
					if(req.status == 1) {
						add_login_error("email", req.msg);
						$('._ptuser_register').text('提交注册')
					} else if(req.status == 2) {
						add_login_error("passwd", req.msg);
						$('._ptuser_register').text('提交注册')
					} else {
						location.reload();
					}
				}
				return;
			},
			error: function() {
				it_enable(cur, false);
				$('#waitMe').waitMe('hide');
				show_msg({
					msg: '系统异常',
					type: 'error',
					time: 1000
				});
			}
		});

	});

	// 通过邮箱发起找回密码
	$("._resetpasswd_email").click(function() {

		var user_email = $("#resetmail").val();
		if(user_email == '') {
			add_mailpasswd_error('resetmail', '请输入邮箱');
			return;
		}
		if(!checkEmail(user_email)) {
			add_mailpasswd_error('resetmail', '请输入正确的邮箱');
			return;
		}

		var cur = $(this);
		// 按钮置为不可点击
		it_enable(cur, true);
		$('#register').text('');
		run_waitMe('#register');

		data = {
			'email': user_email
		};

		$.ajax({
			url: '/member/getpassword.html',
			type: 'post',
			dataType: 'json',
			async: true,
			data: data,
			success: function(req) {
				it_enable(cur, false);
				$('#register').waitMe('hide');
				$('#register').text('提  交');
				if(req.status == 200) {
					// 重置密码邮件发送成功
					var email = req.data;
					location.href = "/member/resetmailsuccess.html?email=" + email;
				} else {
					if(req.status == 1) {
						add_login_error("resetmail",
							req.msg);
					} else {
						show_msg({
							msg: req.msg,
							type: 'error',
							time: 1500
						});
					}
				}
				return;
			},
			error: function() {
				it_enable(cur, false);
				$('#register').waitMe('hide');
				show_msg({
					msg: '系统异常',
					type: 'error',
					time: 1500
				});
			}
		});
	});

	// 通过邮箱重置密码
	$("._mail_resetpasswd").click(function() {

		var user_email = $("#user_email").text();
		if(user_email == '') {
			show_msg({
				msg: '无法获取邮箱',
				type: 'error',
				time: 1500
			});
			return;
		}
		var passwd1 = $("#firstpwd").val();
		if(passwd1 == '') {
			add_mailpasswd_error('firstpwd', '请输入密码');
		}

		var passwd2 = $('#secondpwd').val();
		if(passwd2 == '') {
			add_mailpasswd_error('secondpwd', '请输入密码');
		}

		if(haserror('_mailpasswd_blur')) {
			return;
		}

		if(!checkPasswd(passwd1)) {
			add_mailpasswd_error('firstpwd', '请输入6-16位字符的密码');
			return;
		}

		if(!checkPasswd(passwd2)) {
			add_mailpasswd_error('secondpwd', '请输入6-16位字符的密码');
			return;
		}

		if(passwd1 != passwd2) {
			add_mailpasswd_error('secondpwd', '请输入相同的密码');
			return;
		}

		// _resetpasswd_email
		var cur = $(this);
		// 按钮置为不可点击
		it_enable(cur, true);
		$('#register').text('');
		run_waitMe('#register');
		data = {
			'user_email': user_email,
			'first_pwd': passwd1,
			'second_pwd': passwd2
		};

		$.ajax({
			url: '/member/resetpwd.html',
			type: 'post',
			dataType: 'json',
			async: true,
			data: data,
			success: function(req) {
				it_enable(cur, false);
				$('#register').waitMe('hide');
				$('#register').text('提  交');

				if(req.status == 200) {
					// 重置密码邮件发送成功
					var email = req.data;
					location.href = "/member/resetsuccess.html";
				} else {
					if(req.status == 1) {
						add_login_error("resetmail", req.msg);
					} else {
						show_msg({
							msg: req.msg,
							type: 'error',
							time: 1500
						});
					}
				}
				return;
			},
			error: function() {
				it_enable(cur, false);
				$('#register').waitMe('hide');
				$('#register').text('提  交');
				show_msg({
					msg: '系统异常',
					type: 'error',
					time: 1500
				});
			}
		});

	});

	function it_enable(cur, enable) {
		if(enable) {
			cur.attr("value", cur.attr('tip2'));
			cur.attr("disabled", "true");
		} else {
			cur.attr("value", cur.attr('tip1'));
			cur.removeAttr("disabled");
		}
	}

	// 用户登录成功以后调整页面
	function login_success_direct(ut) {
		if(ut == 2) {
			location.href = "/apiworks/index";
		} else if(ut == 1) {
			//修改1 判断是否是从别的页面跳转过来的
			if(window.location.search) {
				var url = window.location.search.substr(1)
				location.href = url
			} else {
				location.href = "/Index/index";
			}
		}
	}

	// 用户注册成功以后 跳转
	function register_success_direct(email) {
		location.href = "/member/mailsuccess.html?email=" + email;
	}

	// 修改密码，数据输入不符合规范显示错误提示(失去焦点事件)
	function add_resetpasswd_error(id, msg) {
		$("#" + id).addClass("warning");
		$('#' + id + '_error_msg').text(msg);
		$('#' + id + '_error_id').show();
	}

	// 修改密码，移除错误提示(获取焦点事件)
	function rm_resetpasswd_error(id, msg) {
		$("#" + id).removeClass("warning");
		$('#' + id + '_error_msg').text('');
		$('#' + id + '_error_id').hide();
	}

	//TODO 
	// 用户登录、注册，数据输入不符合规范显示错误提示(失去焦点事件)
	function add_login_error(id, msg) {
		$("#" + id).addClass("warning");
		$('#' + id + '_error_id').show();
		$('#' + id + '_error_flag').attr("class", "clear_2");
		$('#' + id + '_error_msg').text(msg);
	}

	// 用户登录注册，移除错误提示(获取焦点事件)
	function rm_login_error(id, msg) {
		$("#" + id).removeClass("warning");
		$('#' + id + '_error_id').hide();
		$('#' + id + '_error_flag').attr("class", "clear_1");
		$('#' + id + '_error_msg').text('');
	}

	// 验证码错误提示添加和移除
	function yzm_login_error(show) {
		if(show) {
			$('#yzm_id').show();
		} else {
			$('#yzm_id').hide();
		}
	}

	// 判断当前页面的需要校验的输入框是否包含 warning
	function haserror(id) {
		var haserror = false;
		$("." + id).each(function() {
			var has = $(this).hasClass("warning");
			if(has) {
				haserror = true;
				return false;
			}
		});
		return haserror;
	}
	// todo 埋点
	$(document).on('click', 'a.zc', function() {
		zhuge.track('点击注册')
	}).on('click', '._registerBef', function() {
		zhuge.track('登录_去注册')
	})

});